let tableIns;
let tree;
let startDateIns;
let endDateIns;
layui.use(['element', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    let form = layui.form;//select、单选、复选等依赖form
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let laydate = layui.laydate;
    tree = layui.tree;
    let height = document.documentElement.clientHeight - 60;

    tableIns = table.render({
        elem: '#quarterListTable'
        , url: ctx + '/cpm/commission/quarterlist/page'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , toolbar: '#quarterListTableToolbar'
        , title: '代理商季度待结分润'
        , cols: [[
            {type:'checkbox'}
            , {field: 'serial', width:200, title: '结算单号'}
            , {field: 'sellerName',title: '商户名称'}
            , {field: 'agentName',title: '代理商名称'}
            , {field: 'transAmount',title: '交易金额', align:'right', style:'font-weight:bold',
            	templet: function(d){return formatMoney(d.transAmount);}}
            , {field: 'stepAmount',title: '季结分润', align:'right', style:'font-weight:bold',
            	templet: function(d){return formatMoney(d.stepAmount);}}
            , {field: 'createTime', width:200, title: '结算时间'}
            , {fixed: 'right', width:150, title: '操作', toolbar: '#quarterListTableBar'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
    });

    //头工具栏事件
    table.on('toolbar(display)', function (obj) {
        switch (obj.event) {
            case 'paymentSel':
            	var checkStatus = table.checkStatus(obj.config.id)
            		,data = checkStatus.data;	// 获取选中的数据
            	// 获取lay-event对应的值
            	var event = obj.event;
            	// 获取其中选中的主表id
            	var ids=[];
            	for(var i = 0; i < data.length; i++){
            		ids.push(data[i].serial);
            	}
            	if(ids.length == 0){
            		layer.msg("请至少选择一条结算项!", {icon: 5});
            		break;
            	}
            	layer.confirm('确认发放季度分佣吗？', function (index) {
            	$.post(ctx + "/cpm/commission/quarterlist/payquarter", {ids:ids.toString(),type:'N'}, function (data) {
                	if(data.flag){
            	        layer.msg("保存成功!", {icon: 1});
                	} else {
                		layer.msg("保存失败!", {icon: 5}); 
                	}
                	$("#queryBtn").click();
                });
            	});
                break;
            case 'query':
                let queryBySellerName = $("#queryBySellerName").val();
                let queryByAgentName = $("#queryByAgentName").val();
                let queryByStartDate = $("#queryByStartDate").val();
                let queryByEndDate = $("#queryByEndDate").val();
                
                table.reload('quarterListTable', {
                    url:'/cpm/commission/quarterlist/page'
                    , where: {
                    	sellerName: queryBySellerName
           			 , agentName: queryByAgentName
           			 , startDate: queryByStartDate
           			 , endDate: queryByEndDate
                    }
                    , page: {
                        curr: 1
                    }
                });
                
                $("#queryBySellerName").val(queryBySellerName);
                $("#queryByAgentName").val(queryByAgentName);
                $("#queryByStartDate").val(queryByStartDate);
                $("#queryByEndDate").val(queryByEndDate);
                
                //日期选择器
                laydate.render({
                    elem: '#queryByStartDate',
                    format: "yyyy-MM-dd",
                    trigger: 'click'
                });
                
            	//日期选择器
                laydate.render({
                    elem: '#queryByEndDate',
                    format: "yyyy-MM-dd",
                    trigger: 'click'
                });
                break;
        }
    });

    //监听行工具事件
    table.on('tool(display)', function (obj) {
        let data = obj.data;
        //确认发放
        if (obj.event === 'paymentOne') {
            layer.confirm('确认发放季度分佣吗？', function (index) {
                $.post(ctx + "/cpm/commission/quarterlist/payquarter", {ids:data.serial,type:'N'}, function (data) {
                	if(data.flag){
            	        layer.msg("保存成功!", {icon: 1});
                	} else {
                		layer.msg("保存失败!", {icon: 5}); 
                	}
                	$("#queryBtn").click();
                });
            });
        }
        //取消发放
        else if (obj.event === 'cancel') {
        	layer.confirm('确认取消季度分佣发放吗？', function (index) {
                $.post(ctx + "/cpm/commission/quarterlist/payquarter", {ids:data.serial,type:'D'}, function (data) {
                	if(data.flag){
            	        layer.msg("保存成功!", {icon: 1});
                	} else {
                		layer.msg("保存失败!", {icon: 5}); 
                	}
                	$("#queryBtn").click();
                });
            });
        }
    });

    //日期选择器
    startDateIns = laydate.render({
        elem: '#queryByStartDate',
        format: "yyyy-MM-dd",
        trigger: 'click'
    });
    
	//日期选择器
    endDateIns = laydate.render({
        elem: '#queryByEndDate',
        format: "yyyy-MM-dd",
        trigger: 'click'
    });
});

